#include "ParticleManager.h"
#include "../Core/MessageSystem.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"

ParticleManager* ParticleManager::GetInstance(void)
{
	static ParticleManager instance;
	return &instance;
}

void ParticleManager::Init()
{
	int id = CSGD_TextureManager::GetInstance()->LoadTexture("resources/Particles/ParticlePic2.png");
	particles[PARTICLE_BLOOD].XMLLoad("resources/Particles/newBlood2.xml", id);
	particles[PARTICLE_BLOOD].SetLooping(false);

	particles[PARTICLE_CASTLE].XMLLoad("resources/Particles/castleUpgrade.xml", id);
	particles[PARTICLE_CASTLE].SetLooping(true);
	particles[PARTICLE_CASTLE].SetColor(D3DCOLOR_ARGB(175, 255, 215, 0));

	particles[PARTICLE_RAIN].XMLLoad("resources/Particles/rainstorm.xml", id);
	particles[PARTICLE_RAIN].SetLooping(true);
	particles[PARTICLE_RAIN].SetColor(D3DCOLOR_ARGB(175, 0, 0, 255));

	particles[PARTICLE_SNOW].XMLLoad("resources/Particles/snow.xml", id);
	particles[PARTICLE_SNOW].SetLooping(true);
	particles[PARTICLE_SNOW].SetColor(D3DCOLOR_ARGB(175, 247, 245, 240));
}


void ParticleManager::Shutdown()
{
	for(unsigned i = 0; i < activeParticles.size(); ++i)
	{
		activeParticles[i]->UnLoad();
		delete activeParticles[i];
	}

	particles[PARTICLE_BLOOD].UnLoad();
}


void ParticleManager::RemoveEmitter( CParticleEmitter* emitterToDelete )
{
	//	iterate through active Emitters

	//	find emitter and remove it from vector
}

void ParticleManager::Update(float dt)
{
	//float dt = CTimer::GetInstance()->GetDeltaTime();
	for(unsigned i = 0; i < activeParticles.size(); ++i)
	{
		activeParticles[i]->Update(dt);
	}

	for(unsigned i = 0; i < activeParticles.size(); ++i)
	{
		if(activeParticles[i]->GetBurstTimer() <= 0)
		{
			activeParticles[i]->UnLoad();
			delete activeParticles[i];
			activeParticles.erase(activeParticles.begin()+i);
		}
	}
}

CParticleEmitter* ParticleManager::CreateEmitter(int type)
{
	CParticleEmitter* emitterToAdd  = new CParticleEmitter;
	particles[type].CopyMeTo(emitterToAdd);

	return emitterToAdd;
}

void ParticleManager::Render()
{
	for(unsigned i = 0; i < activeParticles.size(); ++i)
	{
		activeParticles[i]->Render();
	}
}
